/**
 * 
 */
package dms.presentation.action.global;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.hibernate.SessionFactory;
import org.hibernate.stat.SecondLevelCacheStatistics;
import org.hibernate.stat.Statistics;

import dms.model.Building;
import dms.util.HibernateUtil;

/**
 * @author 李健
 *
 */
public class HibernateStatisticAction extends MappingDispatchAction {
	
	public ActionForward statistic(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		StringBuilder builder = new StringBuilder(300);
		SessionFactory sf = HibernateUtil.getSessionFactory();
		Statistics stat = sf.getStatistics();
		String[] regions = stat.getSecondLevelCacheRegionNames();
		for (String region : regions) {
			SecondLevelCacheStatistics s = stat
					.getSecondLevelCacheStatistics(region);
			builder.append("<h3>");
			builder.append(region);
			builder.append("</h3>");
			builder.append("<p>");
			builder.append(s.toString());
			builder.append("</p>");
		}
		HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
		Building building = (Building) HibernateUtil.getSessionFactory().getCurrentSession().load(Building.class, 1L);
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().print(builder.toString());
		return null;
	}
}
